using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace TemperatureMicro
{
    class Regulator
    {
        private OutputPort _greenLed;
        private OutputPort _redLed;
        private int _id;
        private bool _activeRegulating;
        private int _TargetTemp;
        private Configuration _fileAccess;
        private ServerAccess _serverAccess;

        public Regulator(string macAdress, Cpu.Pin green, Cpu.Pin red, bool regulate, Configuration fileSystem, ServerAccess server)
        {
            _fileAccess = fileSystem;
            _serverAccess = server;
            if (Configuration.RegulatorConfigFilePresent())
            {
                SetId(_fileAccess.GetId(DeviceType.regulator));
            }
            else
            {
                SetId(_serverAccess.registerDevice("Regulator", macAdress));
                _fileAccess.SaveRegulatorId(GetId());
            }

            _greenLed = new OutputPort(green, false);
            _redLed = new OutputPort(red, false);
            _activeRegulating = regulate;
        }

        private void SetId(string id)
        {
            try
            {
                _id = Int32.Parse(id);
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
            }
        }

        public int GetId()
        {
            return _id;
        }

        private void SetTargetTemp(string input)
        {
            if (input == "OFF")
            {
                StopRegulator();
            }
            else
            {
                _TargetTemp = Int32.Parse(input);
                StartRegulator();
            }
        }

        private void StartRegulator()
        {
            _activeRegulating = true;
        }

        private void StopRegulator()
        {
            _activeRegulating = false;
        }

        private double ReadCurrentTemperature()
        {
            var temp = _serverAccess.GetCurrentTemperature(_id.ToString());         //NEED TO CHECK THE WEB API!!!!!
            if (temp == "Failed")
            {
                return _TargetTemp;
            }
            else
            {
                return double.Parse(temp);
            }
        }

        public void Regulate()
        {
            SetTargetTemp(_serverAccess.GetTargetTemperature(_id.ToString()));      //NEED TO CHECK THE WEB API!!!!!
            if (_activeRegulating)
            {
                var currentTemp = ReadCurrentTemperature();
                if (currentTemp + 1 < _TargetTemp)
                {
                    StartHeating();
                }
                else if (currentTemp - 1 > _TargetTemp)
                {
                    StartCooling();
                }
                else
                {
                    StopRegulating();
                }
            }
            else
            {
                StopRegulating();
            }
        }

        private void StartHeating()
        {
            _greenLed.Write(false);
            _redLed.Write(true);
            Debug.Print("Heating!");
        }

        private void StartCooling()
        {
            _greenLed.Write(true);
            _redLed.Write(false);
            Debug.Print("Cooling!");
        }

        private void StopRegulating()
        {
            _greenLed.Write(false);
            _redLed.Write(false);
            Debug.Print("Regulation on target!");
        }
    }
}
